﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace AUV5.Web.Pages
{
    public partial class MessageBox : PageBaseFrontend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (base.IsVerified)
                {
                    //未读公告
                    IList<Model.Bulletin> list = BLL.BulletinBLL.Instance.GetUnReadBulletin(base.VerifiedUserID);
                    int bulletincount = list.Count;
                    if (bulletincount != 0)
                    {
                        lbBulletin.Text = bulletincount.ToString();
                        lbBulletinCount.Text = bulletincount.ToString();
                        lbBulletinCount.Visible = true;
                        repBulletin.DataSource = list;
                        repBulletin.DataBind();
                    }
                    else
                    {
                        panelBulletin.Visible = false;
                    }


                    if (Request["delete"] != null)
                    {
                        DeleteDialog(Request["delete"].ToString());
                        Response.Redirect("MessageBox.aspx");
                    }

                    GetContactList();
                }
                else
                {
                    Response.Redirect(URL_LOGIN);
                }
            }
        }

        //隐藏与某用户的全部对话
        private void DeleteDialog(string contactid)
        {
            Model.ContactList contact = BLL.ContactListBLL.Instance.GetDetail(contactid);
            contact.IsDel = (int)Common.Enum.auvDel.Del;
            BLL.ContactListBLL.Instance.DeleteDialog(contact);

        }


        public void GetContactList()
        {
            int recordcount = 0;
            string type = Request["type"] ?? "";
            IList<Model.ContactList> contactlist = BLL.ContactListBLL.Instance.GetPagedList(base.VerifiedUserID, type, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, ref recordcount);
            AspNetPager1.RecordCount = recordcount;
            StringBuilder sb = new StringBuilder();
            int contentlength = 23;
            if (contactlist != null)
            {
                for (int i = 0; i < contactlist.Count; i++)
                {
                    Model.Message mes = BLL.MessageBLL.Instance.GetDetail(contactlist[i].LatestMessage);
                    if (mes != null)
                    {
                        Model.UserInfo userfrom = BLL.UserInfoBLL.Instance.GetDetail(mes.Sender);
                        Model.UserInfo userto = BLL.UserInfoBLL.Instance.GetDetail(mes.Receiver);
                        Model.UserInfo contact = BLL.UserInfoBLL.Instance.GetDetail(contactlist[i].ContactUserID);
                        if (userfrom != null && userto != null && contact != null)
                        {
                            sb.Append("<li>");
                            sb.Append("<p class='massageavatar'>");
                            sb.Append("<img src='" + contact.PhotoPath + "' alt='' />");
                            sb.Append("</p>");


                            string sender = "";
                            string receiver = "";
                            string bgcolor = "";
                            if (mes.Sender == base.VerifiedUserID)
                            {
                                sender = "我";
                                receiver = userto.UserName;
                                bgcolor = "grey";
                            }
                            else if (mes.Receiver == base.VerifiedUserID)
                            {
                                receiver = "我";
                                sender = userfrom.UserName;
                                if (BLL.RoleInfoBLL.Instance.GetRoleName(userfrom.RoleID) == MANAGER)
                                {
                                    bgcolor = "red";
                                }
                                else
                                {
                                    bgcolor = "blue";
                                }
                            }
                            sb.Append(string.Format("<p class='icon icon{0}'></p>", bgcolor));
                            sb.Append(string.Format("<div class='massageinfo mar_left massageinfo{0}bg'>", bgcolor));

                            sb.Append(string.Format("<span class='{2}'>{0}</span> 对 <span class='{3}'>{1}</span>说：", sender, receiver, sender == "我" ? "bluetxt bold" : "bold", receiver == "我" ? "bluetxt bold" : "bold"));
                            sb.Append(string.Format("<p class='massageinfotxt'><a href='/Pages/MessageShow.aspx?userID={0}'>", contactlist[i].ContactUserID));
                            sb.Append(string.Format("\"{0}\"", mes.Content.Length > contentlength ? mes.Content.Substring(0, contentlength) + "..." : mes.Content));
                            if (!mes.IsRead)
                            {
                                if (mes.Sender == base.VerifiedUserID)
                                {
                                    sb.Append(string.Format("&nbsp;&nbsp;<img src='{0}' title='{1}' />", "/!images/mes_unread.gif", "对方未读"));
                                }
                                else
                                {
                                    sb.Append(string.Format("&nbsp;&nbsp;<img src='{0}' title='{1}' />", "/!images/mes_new.gif", "新信息"));
                                }

                            }
                            sb.Append("</a></p>");
                            sb.Append(string.Format("<span class='massagetime'>{0}</span>", GetTimeSpan(contactlist[i].UpdateDate)));
                            sb.Append(string.Format("<span class='massagedel'><a href='{0}' onclick='return confirm(\"确定删除与{1}的所有对话吗？\")'>", "/Pages/MessageBox.aspx?delete=" + contactlist[i].ContactID, contact.UserName));

                            sb.Append(string.Format("<img src='/!images/icondel_off.gif' id='imgdel{0}' onmouseover=\"MM_swapImage('imgdel{0}',\'\',\'/!images/icondel_on.gif\',1)\" onmouseout='MM_swapImgRestore()' alt='删除' />", i));
                            sb.Append("</a></span>");
                            sb.Append("</div>");

                        }
                    }

                    sb.Append("</li>");

                }
                ulContactList.InnerHtml = sb.ToString();
            }

        }


        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            GetContactList();
        }
    }
}